#!/bin/sh

set -e

. $(dirname $0)/Config.sh
. $(dirname $0)/utils.sh

error=

button()
{
  gpio=$(dirname $0)/gpio_gfrg200

  isUp=$($gpio button)
  if [ "$isUp" != 1 ]; then
    error="factory_reset button was down at start of test"
    return 1
  fi

  echo "<<< press and hold the button and press enter"
  read x

  isUp=$($gpio button)
  if [ "$isUp" != 0 ]; then
    error="factory_reset button was not down"
    return 1
  fi

  echo "<<< release the button and press enter"
  read x

  isUp=$($gpio button)
  if [ "$isUp" != 1 ]; then
    error="factory_reset button was still down at end of test"
    return 1
  fi

  return 0
}

case "$1" in
  diag | quick)
    if button; then
      echo PASS
    else
      echo FAIL "$error"
    fi
    ;;
  *)
    echo "Usage: $0 {diag|quick}"
    exit 1
esac
